package org.jeecg.modules.wlhy.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.paycenter.enums.BillTypeEnum;
import org.jeecg.modules.wlhy.bill.dto.HyBillDTO;
import org.jeecg.modules.wlhy.bill.dto.HyBillShippingNoteDTO;
import org.jeecg.modules.wlhy.bill.entity.HyBill;
import org.jeecg.modules.wlhy.bill.mapper.HyBillMapper;
import org.jeecg.modules.wlhy.bill.service.IHyBillService;
import org.jeecg.modules.wlhy.hydriverbank.entity.HyDriverBank;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: hy_bill
 * @Author: jeecg-boot
 * @Date:   2022-10-28
 * @Version: V1.0
 */
@Service
public class HyBillServiceImpl extends ServiceImpl<HyBillMapper, HyBill> implements IHyBillService {

    @Override
    public IPage<HyBillDTO> getBillPageList(Page<HyBillDTO> page, QueryWrapper<HyBillDTO> queryWrapper) {
        IPage<HyBillDTO> pageList = baseMapper.getBillPageList(page, queryWrapper);
        return pageList;
    }
    @Override
    public IPage<HyBillDTO> getReqSubFeeShippingPageList(Page<HyBillDTO> page, QueryWrapper<HyBillDTO> queryWrapper, String shippingStatus) {
        IPage<HyBillDTO> pageList = baseMapper.getReqSubFeeShippingPageList(page, queryWrapper, shippingStatus);
        return pageList;
    }

    @Override
    public List<HyBill> listByIdsForUpdate(Collection<? extends Serializable> ids) {
        return baseMapper.selectListByIdsForUpdate(ids);
    }

    @Override
    public BigDecimal getShippingSubFee(String shippingId) {
        return baseMapper.getShippingSubFee(shippingId);
    }

    @Override
    public IPage<HyBillDTO> getPlanFeeReqPageList(Page<HyBillDTO> page, QueryWrapper<HyBillDTO> queryWrapper, String billType) {
        IPage<HyBillDTO> pageList = baseMapper.getPlanFeeReqPageList(page, queryWrapper, billType);
        return pageList;
    }

    @Override
    public List<HyBill> getBillListByShippingIds(List<String> shippingids, String billType) {
        return this.list(new QueryWrapper<HyBill>().eq("bill_type",billType).in("shipping_id",shippingids));
    }

    @Override
    public List<HyBill> getBillListByShippingId(String shippingId){
        return this.list(new QueryWrapper<HyBill>().eq("shipping_id", shippingId));
    }

    @Override
    public HyBill getBillByShippingId(String shippingId, String billType) {
        return this.getOne(new QueryWrapper<HyBill>().eq("bill_type", billType).eq("shipping_id", shippingId));
    }

    @Override
    public boolean checkIsPay(String shippingId, String billType) {
        HyBill bill = this.getBillByShippingId(shippingId, billType);
        if(bill == null){
            return false;
        }
        String payStatus = bill.getPayStatus();
        if("5".equals(payStatus) || "6".equals(payStatus) || "7".equals(payStatus)){
            return true;
        }
        return false;
    }

    @Override
    public boolean checkIsPay(HyBill bill) {
        if(bill == null){
            return false;
        }
        String payStatus = bill.getPayStatus();
        if("5".equals(payStatus) || "6".equals(payStatus) || "7".equals(payStatus)){
            return true;
        }
        return false;
    }

    @Override
    public void deleteBillByShippNoteId(String shippingId, BillTypeEnum billType) {
        HyBill bill = this.getBillByShippingId(shippingId, billType.value);
        if(bill == null){
            return;
        }
        if(this.checkIsPay(bill)){
            throw new JeecgBootException("已支付不能删除");
        }
        this.removeById(bill.getId());
    }

    @Override
    public void deleteBillListByShippNoteId(String shippingId) {
        List<HyBill> list = this.getBillListByShippingId(shippingId);
        if(list == null && list.size() == 0){
            return;
        }
        List<String> ids = new ArrayList<>();
        for (HyBill bill : list) {
            if(this.checkIsPay(bill)){
                throw new JeecgBootException("已支付不能删除");
            }
            ids.add(bill.getId());
        }
        this.removeByIds(ids);
    }

    @Override
    public IPage<HyBillShippingNoteDTO> queryBillPageList(Page<HyBillShippingNoteDTO> page, QueryWrapper<HyBillDTO> queryWrapper) {
        IPage<HyBillShippingNoteDTO> pageList = baseMapper.queryBillPageList(page, queryWrapper);
        return pageList;
    }

    @Override
    public IPage<HyBillDTO> getPreFeeReqList(Page<HyBillDTO> page, QueryWrapper<HyBillDTO> queryWrapper) {
        IPage<HyBillDTO> pageList = baseMapper.getPreFeeReqList(page, queryWrapper);
        return pageList;
    }

    @Override
    public List<HyBillShippingNoteDTO> getBillShippingNoteList(QueryWrapper<HyBillDTO> queryWrapper) {
        return baseMapper.getBillShippingNoteList(queryWrapper);
    }

    @Override
    public boolean updateStatusByShippingId(String shippingId, String status) {
        return this.update(new LambdaUpdateWrapper<HyBill>()
                .eq(HyBill::getShippingId, shippingId)
                .set(HyBill::getShippingStatus, status));
    }

    @Override
    public HyDriverBank getDriverBankByDriverId(String driverId) {
        return baseMapper.getDriverBankByDriverId(driverId);
    }


}
